//主页下拉出现动画
$(".body-div ul>li:nth-child(2)").hover(function () {
    $(this).children("div").slideToggle(200);
});

$(function () {
    //隔行变色
    $(".shopping-list li:even").css("backgroundColor", "#DBDBDB");

    //商品选取数量的减少
    $(".reduce").on("click", function () {
        let selectAmount = $(this).siblings("span").html();
        if (selectAmount > 1) {
            selectAmount--;

            $(this).siblings("span").html(selectAmount);
            let price = $(this).parent(".amount").siblings(".price").children().html().replace("￥", "");
            let goodsAmount = $(this).siblings("span").html();
            $(this).parent(".amount").siblings(".subtotal").children().html("￥" + Number(goodsAmount * price).toFixed(2));

            let goods_name = $(this).parent(".amount").siblings(".goods").children(".detail").children(".goods-name").html();
            let valuesMap = JSON.parse(localStorage.getItem(goods_name));
            valuesMap[4].gwc_selectAmount = selectAmount;
            localStorage.setItem(goods_name, JSON.stringify(valuesMap));
            part_update();
        }
    });

    //商品选取数量的增加
    $(".increase").on("click", function () {
        let selectAmount = $(this).siblings("span").html();
        selectAmount++;

        $(this).siblings("span").html(selectAmount);
        let price = $(this).parent(".amount").siblings(".price").children().html().replace("￥", "");
        let goodsAmount = $(this).siblings("span").html();
        $(this).parent(".amount").siblings(".subtotal").children().html("￥" + Number(goodsAmount * price).toFixed(2));

        let goods_name = $(this).parent(".amount").siblings(".goods").children(".detail").children(".goods-name").html();
        let valuesMap = JSON.parse(localStorage.getItem(goods_name));
        valuesMap[4].gwc_selectAmount = selectAmount;
        localStorage.setItem(goods_name, JSON.stringify(valuesMap));
        part_update();
    });

    //实现购物车删除商品列表
    $(".operation span").on("click", function () {
        $(this).parent(".operation").parent("li").addClass("active");
        let li = $(".shopping-list ul li");
        for (let i = 0; i < li.length; i++) {
            if (li[i].className === "active") {
                li.eq(i).remove();
            }
        }

        let delete_goods_name = $(this).parent(".operation").siblings(".goods").children(".detail").children("span").html();
        localStorage.removeItem(delete_goods_name + "_detail");

        $(".shopping-list li:even").css("backgroundColor", "#DBDBDB");
    });

    //实现购物车列表点击部分商品后增加激活状态
    $(".goods .check input").on("click", function () {
        if ($(this).hasClass("activeCheck")) {
            $(this).removeClass("activeCheck");
            part_update();
        } else {
            $(this).addClass("activeCheck");
            part_update();
        }
    });

    //实现购物车点击部分商品后在结算清单中显示
    function part_update() {
        let part_goods_totalAmount = 0;
        let part_goods_totalPrice = 0;
        let inputList = $(".goods .check input");
        for (let i = 0; i < inputList.length; i++) {
            if ($(inputList[i]).hasClass("activeCheck")) {
                part_goods_totalAmount += Number($(inputList[i]).parent(".check").parent(".goods").siblings(".amount").children("span").html());
                part_goods_totalPrice += Number($(inputList[i]).parent(".check").parent(".goods").siblings(".subtotal").children("span").html().replace("￥", ""));
            }
        }
        $(".settlement .total-amount span:nth-child(2)").html(part_goods_totalAmount);
        $(".settlement .total-price span:last-child").html("￥" + Number(part_goods_totalPrice).toFixed(2));
    }

    //实时监听购物车商品总数量,大于0件商品button按钮则高亮显示
    let listenerAmount = 0;
    $(".settlement .total-amount span:nth-child(2)").bind("DOMNodeInserted", function (e) {
        listenerAmount = $(e.target).html();
        if (listenerAmount !== "0") {
            $(".settlement .settlement-btn button").css("backgroundColor", "#A10000");
        } else {
            listenerAmount = 0;
            $(".settlement .settlement-btn button").css("backgroundColor", "#b3b3b3");
        }
    });

    //实现结算按钮点击结算时,如果未选择商品则提示用户请选择商品
    $(".settlement .settlement-btn button").on("click", function () {
        if (listenerAmount !== 0) {
            let inputList = $(".goods .check input");
            for (let i = 0; i < inputList.length; i++) {
                if ($(inputList[i]).hasClass("activeCheck")) {
                    let pay_goods_img_path = $(inputList[i]).parent().siblings("img").attr("src");
                    let pay_goods_name = $(inputList[i]).parent().siblings(".detail").children(".goods-name").html();
                    let pay_goods_color = $(inputList[i]).parent().siblings(".detail").children(".goods-color").children("span").html();
                    let pay_goods_amount = $(inputList[i]).parent().parent().siblings(".amount").children("span").html();
                    let pay_goods_totalPrice = $(inputList[i]).parent().parent().siblings(".subtotal").children().html();

                    let pay_map = [{ "pay_goods_img_path": pay_goods_img_path }, { "pay_goods_name": pay_goods_name }, { "pay_goods_color": pay_goods_color }, { "pay_goods_amount": pay_goods_amount }, { "pay_goods_totalPrice": pay_goods_totalPrice }];
                    localStorage.setItem(pay_goods_name + "_pay", JSON.stringify(pay_map));
                }
            }
            window.location.href = "../ProgramJSP/order.jsp";
        } else {
            window.setTimeout(function () {
                $(".settlement-tip").css("display", "block");
                window.setTimeout(function () {
                    $(".settlement-tip").css("display", "none");
                }, 2500);
            }, 500);
        }
    });
})

//购物车页面使用数据
for (let i = 0; i < localStorage.length; i++) {
    let keyName = localStorage.key(i);
    if (keyName.indexOf("_detail") !== -1) {
        let valuesMap = JSON.parse(localStorage.getItem(localStorage.key(i)));
        let gwc_imgPath = valuesMap[0].gwc_imgPath;
        let gwc_goodsColor = valuesMap[1].gwc_goodsColor;
        let gwc_goodsName = valuesMap[2].gwc_goodsName;
        let gwc_singlePrice = valuesMap[3].gwc_singlePrice;
        let gwc_selectAmount = valuesMap[4].gwc_selectAmount;
        let priceTemp = gwc_singlePrice.replace("￥", "");
        let liEle = `<li>
                    <div class="goods">
                        <div class="check">
                            <input type="checkbox" class="checkItem">
                        </div>
                        <img src="` + gwc_imgPath + `" alt="">
                        <div class="detail">
                            <span class="goods-name">` + gwc_goodsName + `</span>
                            <span class="goods-color">颜色分类: <span>` + gwc_goodsColor + `</span></span>
                        </div>
                    </div>
                    <div class="price">
                        <span>` + gwc_singlePrice + `</span>
                    </div>
                    <div class="amount">
                        <img class="reduce" src="../img/goktech-img/images/sub.jpg" alt="">
                        <span>` + gwc_selectAmount + `</span>
                        <img class="increase" src="../img/goktech-img/images/add.jpg" alt="">
                    </div>
                    <div class="subtotal">
                        <span>￥` + Number(priceTemp * gwc_selectAmount).toFixed(2) + `</span>
                    </div>
                    <div class="operation">
                        <span>删除</span>
                    </div>
                </li>`;
        let ulEle = $(".shopping-list ul");
        ulEle.append(liEle);

        $(".shopping-list li:even").css("backgroundColor", "#DBDBDB");
    }
}

//购物车全选功能
let flag = 1;
$("#allChoose").on("click", function () {
    $(".checkItem").prop("checked", $("#allChoose").prop("checked"));
    let amountTotal = 0;
    let priceTotal = 0;
    let amountList = $(".amount span");
    let priceList = $(".subtotal span");
    for (let i = 0; i < amountList.length; i++) {
        amountTotal += Number($(amountList[i]).html());
    }
    for (let j = 0; j < priceList.length; j++) {
        priceTotal += Number($(priceList[j]).html().replace("￥", ""));
    }

    let inputList = $(".goods .check input");

    if (flag === 1) {
        flag = 0;
        $(".settlement .total-amount span:nth-child(2)").html(amountTotal);
        $(".settlement .total-price span:last-child").html("￥" + Number(priceTotal).toFixed(2));

        for (let i = 0; i < inputList.length; i++) {
            $(inputList[i]).addClass("activeCheck");
        }
    } else {
        flag = 1;
        $(".settlement .total-amount span:nth-child(2)").html(0);
        $(".settlement .total-price span:last-child").html("￥0.00");

        for (let i = 0; i < inputList.length; i++) {
            $(inputList[i]).removeClass("activeCheck");
        }
    }
})